package com.witmed.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.witmed.dao.DiscussDao;
import com.witmed.entity.Article;
import com.witmed.entity.Discuss;
import com.witmed.entity.UserInfo;
import com.witmed.util.DBUtils;

/**
 * DiscussDao的实现类
 * 
 * @author ChenYouming
 *
 */
public class DiscussDaoImpl implements DiscussDao {
	private DBUtils db =new DBUtils();
			
	
	/**
	 * 实现findDicuss接口
	 */
	@Override
	public List<Discuss> findDicuss(int articleId) {
		String sql = "select d.discuss_id," + 
				"d.discuss_content," + 
				"d.discuss_time," + 
				"u.user_name," + 
				"d.article_id," + 
				"d.repalce_discuss_id," + 
				"u1.user_name un " + 
				"from discuss d " + 
				"LEFT JOIN user u on d.user_id =u.user_id " + 
				"LEFT JOIN article a on d.article_id =a.article_id " + 
				"LEFT JOIN discuss d1 on d.repalce_discuss_id =d1.discuss_id " + 
				"LEFT JOIN user u1 on d1.user_id =u1.user_id"+
				" where d.article_id=? " ; 
				
		ResultSet resultSet = db.query(sql,articleId);
		List<Discuss> list =new ArrayList<Discuss>();
		try {
			while(resultSet.next()) {
				int discussId = resultSet.getInt("discuss_id");
				String discussContent = resultSet.getString("discuss_content");
				String discussTime=resultSet.getString("discuss_time");
				String userName=resultSet.getString("user_name");
			    articleId =resultSet.getInt("article_id");
				int repalceDiscussId =resultSet.getInt("repalce_discuss_id");
				String userName1=resultSet.getString("un");
				UserInfo userInfo =new UserInfo(null, userName, null, null, null, null, null, null, null);
				UserInfo userInfo1 =new UserInfo(null, userName1, null, null, null, null, null, null, null);
				Article article =new Article(articleId, null, null, null, null);
				Discuss discuss =new Discuss(discussId,discussContent,discussTime,userInfo,article,new Discuss(repalceDiscussId,null,null,userInfo1,null,null));
			list.add(discuss);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 实现deleteDicussById接口
	 */
	@Override
	public int deleteDicussById(int id,int userId) {
		String sql ="delete from discuss where discuss_id=? and user_id=?";
		int i = db.update(sql,id,userId);
		return i;
	}

	/**
	 * 新增评论回复其他用户
	 * 
	 * @param discuss
	 * @return 影响行数
	 */
	@Override
	public int insert(Discuss discuss) {
		String sql="INSERT into discuss(discuss_id,discuss_content,discuss_time,user_id,article_id,repalce_discuss_id) VALUES(null,?,?,?,?,?)";
		int i = db.update(sql, discuss.getDiscussContent(),discuss.getDiscussTime(),discuss.getUser().getUserId(),discuss.getArticle().getArticleId(),discuss.getDiscuss().getDiscussId());
		return i;
	}

	/**
	 * 新增评论回复当前文章
	 * 
	 * @param discuss
	 * @return 影响行数
	 */
	@Override
	public int insertToArticle(Discuss discuss) {
		String sql="INSERT into discuss VALUES(null,?,?,?,?,null)";
		int i = db.update(sql, discuss.getDiscussContent(),discuss.getDiscussTime(),discuss.getUser().getUserId(),discuss.getArticle().getArticleId());
		return i;
	}
	
}
